package com.csgc.camera.widget.dialog

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.ObjectAnimator
import android.content.Context
import android.view.Gravity
import android.widget.ProgressBar
import androidx.annotation.StyleRes

import com.csgc.camera.widget.dialog.base.AnimAction
import com.csgc.camera.widget.dialog.base.BaseDialog
import com.cysd.spsyxj.R


class RemoveMaskLoadingDialog constructor(context: Context, @StyleRes themeResId: Int = R.style.BaseDialogTheme):
    BaseDialog(context,themeResId)  {
    private var mObjectAnimator: ObjectAnimator? = null
    private val pbLoading: ProgressBar? by lazy { findViewById(R.id.pb_loading) }
    var listener:OnLoadEndListener?=null

    init {
        setContentView(R.layout.dialog_remove_mask_loading)
        setWindowAnimations(AnimAction.ANIM_IOS)
        setGravity(Gravity.CENTER)
        setCancelable(false)
        setOnDismissListener {
            mObjectAnimator?.cancel()
        }
    }

    fun showDialog(){
        show()
        showProgressLoading()
    }

    private fun showProgressLoading() {
        if (null != mObjectAnimator && mObjectAnimator!!.isRunning) mObjectAnimator?.cancel()
        mObjectAnimator = ObjectAnimator.ofInt(pbLoading, "progress", 0, 100)
        mObjectAnimator?.duration = 3000
        mObjectAnimator?.addListener(object :AnimatorListener{
            override fun onAnimationStart(p0: Animator) {

            }

            override fun onAnimationEnd(p0: Animator) {
                dismiss()
                listener?.onLoadEnd()
            }

            override fun onAnimationCancel(p0: Animator) {

            }

            override fun onAnimationRepeat(p0: Animator) {

            }
        })
        mObjectAnimator?.start()
    }

    interface OnLoadEndListener{
        fun onLoadEnd()
    }
}